var fs = require('fs'); 
import {GameGlobalConfigDef} from "./global_config_def"

export  class GlobalConfig extends GameGlobalConfigDef{

    public static init() {
        var files = fs.readdirSync(__dirname);
        let jsonData = {}
        for(var key in files)
        {
            var file=files[key];
            var searchIndex=file.lastIndexOf(".json");
            if(searchIndex>0)
            {
                var fileName=file.substring(0,searchIndex);
                var config= require("./"+file);
                jsonData[fileName]=config;
            }
        }
		GlobalConfig.setIns(jsonData);
    }

    private static _instance: any=null;

	public static ins(): GlobalConfig {
        if(!this._instance){
            GlobalConfig.init();
        }
        return this._instance;
    }

    public static setIns  (value) {
        if (!GlobalConfig._instance) {
            this._instance = value;
        }
        else {
            throw new Error("配置文件应该只设置一次");
        }
    };

    public static findBy(){

    }
    public static getFirst(data){
        let res=null;
        for(let key in data){
            res=data[key];
            break;
        }
        return res;
    }


}